home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mission 3
/
Mission 3.zip
/
Mission 3.iso
/
demovers
/
scripter
/
libs
/
math.lib
< prev
next >
Wrap
Text File
|
1998-09-26
|
3KB
|
219 lines
proc sin(x)
local i, n, t, x2, p, s;
{
x = float(x);
if (x < 0) i = 1, x = -x;
n = round(x / (p = pi()));
x -= p * n;
if (n % 2) x = -x;
t = x;
x2 = - x * x;
for (n = 3.0; ; n += 2.0) {
s = x + (t *= x2 / (n * (n - 1.0)));
if (x == s) return i ? -s : s;
x = s;
}
}
proc cos(x)
{
return sin(x + pi() / 2);
}
proc tan(x)
local d;
{
if ((d = cos(x)) == 0) throw("bad value", x);
return sin(x) / d;
}
proc cot(x)
local d;
{
if ((d = sin(x)) == 0) throw("bad value", x);
return cos(x) / d;
}
proc arctan(x)
local a, i, n, pi4, s, t, z;
{
x = float(x);
if (x < 0) i = 1, x = -x;
pi4 = 0.25 * pi();
while (x >= 0.415) a += pi4, x = (x - 1.0) / (x + 1.0);
t = x;
z = - x * x;
for (n = 3.0; ; n += 2.0) {
s = x + (t *= z) / n;
if (s == x) {
s += a + (t * z) / (n + 2);
return i ? -s : s;
}
x = s;
}
}
proc arcsin(x)
{
if (x < -1 || x > 1) throw("bad value", x);
if (x == 1 || x == -1) return 0.5 * pi();
return arctan(x / sqrt(1 - x*x));
}
proc arccos(x)
{
if (x < -1 || x > 1) throw("bad value", x);
if (x == -1) return pi();
if (x == 1) return 0.0;
return 0.5 * pi() - arctan(x / sqrt(1 - x*x));
}
proc arccot(x)
{
return 0.5 * pi() - arctan(x);
}
proc sinh(x)
local e;
{
e = exp(x);
return 0.5 * (e - 1/e);
}
proc cosh(x)
local e;
{
e = exp(x);
return 0.5 * (e + 1/e);
}
proc tanh(x)
local e, f;
{
e = exp(x);
f = 1 / e;
return (e - f) / (e + f);
}
proc coth(x)
local e, f;
{
if (x == 0) throw("bad value", x);
e = exp(x);
f = 1 / e;
return (e + f) / (e - f);
}
proc arsinh(x)
{
x = float(x);
return ln(x + sqrt(x*x + 1));
}
proc arcosh(x)
{
x = float(x);
if (x < 1) throw("bad value", x);
return ln(x + sqrt(x*x - 1));
}
proc artanh(x)
{
if (x <= -1 || x >= 1) throw("bad value", x);
return 0.5 * ln((1 + x) / (1 - x));
}
proc arcoth(x)
{
x = float(x);
if (x >= -1 && x <= 1) throw("bad value", x);
return 0.5 * ln((x + 1) / (x - 1));
}
proc exp(x)
local d, f, n, p, q, s, y;
{
x = float(x);
if (x < 0) q = 1, x = -x;
if (x > 1) x /= (d = float("1e" + string(exponent(x) + 1)));
y = x + 1;
f = 1.0;
p = x;
for (n = 2.0; ; n += 1.0) {
s = y + (p *= x) / (f *= n);
if (y == s) {
s += (p *= x) / (f *= (n + 1.0));
s += (p * x) / (f * (n + 2.0));
if ((d = int(d)) > 0) s = pow(s, d);
return q ? 1 / s : s;
}
y = s;
}
}
proc ln(x)
local f, n, s, t, y;
{
x = float(x);
if (x <= 0) throw("bad value", x);
f = 2.0;
while (x >= 2) f = f + f, x = sqrt(x);
while (x <= 0.5) f = f + f, x = sqrt(x);
t = y = (x - 1) / (x + 1);
x = t * t;
for (n = 3.0; ; n += 2.0) {
s = y + (t *= x) / n;
if (y == s) return f * s;
y = s;
}
}
proc log10(x)
{
if (x <= 0) throw("bad value", x);
return ln(x) / ln(10.0);
}
proc sqrt(x)
local e, y, yn;
{
if (x < 0) throw("bad value", x);
if (x == 0) return 0.0;
e = exponent(x) + 1;
y = e & 1 ? 1.581 : 0.5;
if (e >>= 1) y *= float("1e" + string(e));
forever {
yn = 0.5 * (y + x/y);
if (yn == y) return yn;
y = yn;
}
}
proc pow(x, a)
local q, y;
{
x = float(x);
if (a.type != "integer") return exp(a * ln(x));
a = int(a);
if (a < 0) a = -a, q = 1;
y = 1.0;
for (; a; a >>= 1) {
if (a & 1) y *= x;
x = x * x;
}
return q ? 1/y : y;
}